package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.utils.QiniuUtils;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author zhengkaiyuan
 * @version 1.0
 * @create 2021-06-21 19:27
 */
@RestController
@RequestMapping("/Setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;



    // imgFile:需要跟页面el-upload里面的name保持一致
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            String filename = imgFile.getOriginalFilename();
            // 找到.最后出现的位置
            int index = filename.indexOf(".");
            //获取文件后缀
            String suffix  = filename.substring(index);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
//            =============添加到七牛云空间,但没有进入数据库，保存到redis中===================
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
//            =============================================================================
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
            return result;
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,@RequestParam("travelgroupIds") Integer[] travelGroups){
        try {
            setmealService.add(setmeal,travelGroups);
//            =============添加到七牛云空间,并且进入数据库，保存到redis中===================
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
//            =============================================================================
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult setmealPage= null;
        try {
            setmealPage = setmealService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return setmealPage;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
            return  new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(true,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/findTravelgroupBySetmealId")
    public Result findTravelgroupBySetmealId(Integer id){
        try {
            List<Integer> integerList= setmealService.findTravelgroupBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,integerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] travelGroupIds){
        try {
            setmealService.edit(setmeal,travelGroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }


    @RequestMapping("/downLoad")
    public Result downLoad(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request) {
        //获取下载地址
        try {
            String path = request.getSession().getServletContext().getRealPath("/other/setmeal");
            String excelName = setmealService.downLoadFile(path, queryPageBean.getQueryString());
            return new Result(true,MessageConstant.DOWN_LOAD_DATE_SUCCESS,excelName);
        }catch (RuntimeException e){
            // 运行时异常，表示自由行和跟团游的关联表中存在数据
            return new Result(false,e.getMessage());
        }catch (Exception e){
            // 运行时异常，表示自由行和跟团游的关联表中存在数据
            return new Result(false,e.getMessage());
        }
    }

}
